package com.itheima.controller;


import com.itheima.pojo.User;
import com.itheima.service.UserServer;
import com.itheima.utils.JwtUtil;
import com.itheima.utils.Md5Util;
import com.itheima.utils.ThreadLocalUtil;
import jakarta.validation.constraints.Pattern;

import org.hibernate.validator.constraints.URL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.itheima.pojo.Result;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户
 */
@RestController
@RequestMapping("/user")
@Validated

public class UserConrtoller {

    @Autowired
    private UserServer userServer;


    /**
     * 查询用户名是否被占用
     */
    @PostMapping("/register")
    public Result register(@Pattern(regexp = "^\\S{5,16}$") String username, @Pattern(regexp = "^\\S{5,16}$") String password) {

        /**
         * 判断用户和密码是否为空
         * */
       /* if(username!=null && username.length()>=5 && username.length()<=16
        && password!=null &&password.length()>=5 && password.length()<=16
        ){*/
        /* }else {
            return Result.error("参数不合法");
        }*手动校验/
            /*查询用户*/
        User u = userServer.findByUserName(username);
        if (u == null) {
            //没有被占用
            //注册
            userServer.register(username, password);
            return Result.success();
        } else {
            //被占用

            return Result.error("用户被占用");
        }
    }

    @PostMapping("/login")
    public Result<String> login(@Pattern(regexp = "^\\S{5,16}$") String username, @Pattern(regexp = "^\\S{5,16}$") String password) {
        //根据用户名查询用户
        User loginUser = userServer.findByUserName(username);

        //判断该用户是否存在
        if (loginUser == null) {
            return Result.error("用户名错误");
        }
        //判断密码是否正确
        if (Md5Util.getMD5String(password).equals(loginUser.getPassword())) {

            Map<String, Object> claims = new HashMap<>();
            claims.put("id", loginUser.getId());
            claims.put("username", loginUser.getUsername());
            String token = JwtUtil.genToken(claims);
            return Result.success(token);
        }

        return Result.error("密码错误");

    }

    @GetMapping("/userInfo")
    public Result<User> userInfo(@RequestHeader(name = "Autorization") String token) {
        //根据用户名查询用户
       /* Map<String, Object> map = JwtUtil.parseToken(token);
        String username = (String) map.get("username");*/

        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("usernaem");
        User user = userServer.findByUserName(username);
        return Result.success(user);

    }

    /*

     */
    @PutMapping("/update")
    public Result update(@RequestBody @Validated User user) {
        userServer.update(user);
        return Result.success();
    }


    @PatchMapping("/user/updateAvatar")
    public Result Avatar(@RequestParam @URL String avatarUrl) {
        userServer.updateAvater(avatarUrl);
        return Result.success();
    }

    @PatchMapping("/user/updatePwd")
    public Result updatePwd(@RequestBody Map<String,String>Params) {
        String oldPwd = Params.get("old_pwd");
        String newPwd = Params.get("new_pwd");
        String rePwd = Params.get("re_pwd");
        if (!StringUtils.hasLength(oldPwd) || !StringUtils.hasLength(newPwd) || !StringUtils.hasLength(rePwd)) {
            return Result.error("缺少参数！！");

        }
        Map<String, Object> map = ThreadLocalUtil.get();
        String username = (String) map.get("username");
        User loginUser = userServer.findByUserName(username);
        if (!loginUser.getPassword().equals(Md5Util.getMD5String(oldPwd))){
            return Result.error("原密码错误");

        }


        if (!rePwd.equals(newPwd)) {
            return Result.error("两次填写的密码不一致");

        }
        userServer.updatePwd(newPwd);
        return Result.success();
    }
}
